package com.monai.aidati.aiservice;


import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionChoice;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import okhttp3.ConnectionPool;
import okhttp3.Dispatcher;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class ChatCompletionsExample {
    static String apiKey = "6c9c28b8-66c0-4d47-8ae6-d99231643803";
    static ConnectionPool connectionPool = new ConnectionPool(5, 1, TimeUnit.SECONDS);
    static Dispatcher dispatcher = new Dispatcher();
    static ArkService service = ArkService.builder().dispatcher(dispatcher).connectionPool(connectionPool).baseUrl("https://ark.cn-beijing.volces.com/api/v3").apiKey(apiKey).build();
    public static  String run(String message) {
        final List<ChatMessage> messages = new ArrayList<>();
        final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content(message).build();
        final ChatMessage systemMessage= ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是一个数据分析师和前端开发专家，接下来我会按照以下固定格式给你提供内容：\n" +
                "分析需求：{数据分析的需求或者目标}\n" +
                "请根据这部分内容，按照以下指定格式生成内容（此外不要输出任何多余的开头、结尾、注释）\n" +
                "{前端 Echarts V5 的 option 配置对象js代码，合理地将数据进行可视化，不要生成任何多余的内容，比如注释}此外还需要生成结论并且图形形式要以我传输给你的为准,并且返回的数据要符合json的格式").build();
        messages.add(userMessage);
        messages.add(systemMessage);
        ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
                .model("ep-20250108221109-kbvqk")
                .messages(messages)
                .build();
        // 发送请求并获取响应
        List<ChatCompletionChoice> choices = service.createChatCompletion(chatCompletionRequest).getChoices();
        StringBuffer stringBuffer=new StringBuffer();
        // 遍历choices并逐个打印
        for (ChatCompletionChoice choice : choices) {
            String content = (String) choice.getMessage().getContent();
            stringBuffer.append(content);
        }
        service.shutdownExecutor();
        String resultContent=new String(stringBuffer);
        return resultContent;
    }

}